package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.EbookEntry;
import com.qf.mapper.EbookEntryMapper;
import com.qf.service.EbookEntryServic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 10537
 * @version V1.0
 * @Date 2021/10/5 10:16
 */
@Service
public class EbookEntryServicImpl implements EbookEntryServic {
    @Autowired
    EbookEntryMapper ebookEntryMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void add(EbookEntry ebookEntry) {
        int i = ebookEntryMapper.insert(ebookEntry);
        if (i!=1){
            System.out.println("添加图书信息失败");
            throw new RuntimeException("添加图书信息失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void remove(Integer id) {
        int i = ebookEntryMapper.deleteByPrimaryKey(id);
        if (i!=1){
            System.out.println("删除图书信息失败");
            throw new RuntimeException("删除图书信息失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void modify(EbookEntry ebookEntry) {
        int i = ebookEntryMapper.updateByPrimaryKey(ebookEntry);
        if (i!=1){
            System.out.println("修改图书信息失败");
            throw new RuntimeException("修改图书信息失败");
        }
    }

    @Override
    public EbookEntry findById(Integer id) {
        return ebookEntryMapper.selectById(id);
    }

    @Override
    public PageInfo<EbookEntry> findByCategoryAndPage(Integer page, Integer rows, Integer ebookCategory) {
        PageHelper.startPage(page,rows);
        List<EbookEntry> ebookEntries = ebookEntryMapper.selectByCategoryId(ebookCategory);
        PageInfo<EbookEntry> pageInfo=new PageInfo<>(ebookEntries);
        return pageInfo;
    }
}
